package com.mMap.controller;

import com.mMap.annotation.GlobalInterceptor;
import com.mMap.annotation.VerifyParam;
import com.mMap.entity.constants.Constants;
import com.mMap.entity.dto.SessionWebUserDto;
import com.mMap.entity.enums.VerifyRegexEnum;
import com.mMap.entity.po.MapInfo;
import com.mMap.entity.po.UserInfo;
import com.mMap.entity.query.MapInfoQuery;
import com.mMap.entity.query.UserInfoQuery;
import com.mMap.entity.vo.ResponseVO;
import com.mMap.exception.BusinessException;
import com.mMap.service.UserInfoService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;


@RestController
public class UserController extends ABaseController{
    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("editUserInfo")
    @GlobalInterceptor(checkParams= true)
    public ResponseVO register(HttpSession session,
                               @VerifyParam(required = false, regex = VerifyRegexEnum.EMAIL,max = 150) String email,
                               @VerifyParam(required = false) String nickName,
                               @VerifyParam(required = false, regex = VerifyRegexEnum.PASSWORD, min = 8, max = 18) String password,
                               @VerifyParam(required = false) String role,
                               @VerifyParam(required = false) String phone
    ) {
        try {
//            userInfoService.updateUserInfoByUserId();
            SessionWebUserDto webUserDto = getUserInfoFromSession(session);
            System.out.println(webUserDto);
            UserInfo userInfo = new UserInfo();
            userInfo.setEmail(email);
            userInfo.setNickName(nickName);
            userInfo.setPassword(password);
            userInfo.setRole(Integer.valueOf(role));
            userInfo.setPhone(phone);
            System.out.println(userInfo);
            userInfoService.updateUserInfoByUserId(userInfo, webUserDto.getUserId());
//            webUserDto.setAvatar(null);
            webUserDto.setEmail(email);
            webUserDto.setNickName(nickName);
//            webUserDto.setPassword(password);
            webUserDto.setRole(Integer.valueOf(role));
            webUserDto.setPhone(phone);
            session.setAttribute(Constants.SESSION_KEY, webUserDto);
            return getSuccessResponseVO(webUserDto);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }
    @CrossOrigin(origins = Constants.FrontOrigins, allowCredentials = "true")
    @RequestMapping("userList")
    @GlobalInterceptor(checkLogin=true,checkParams= true)
    public ResponseVO mapList(HttpSession session,@RequestBody UserInfoQuery query
    ) {
        try {
            SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
//            System.out.println(sessionWebUserDto.getUserId());
//            if (sessionWebUserDto.getRole()==0||sessionWebUserDto.getRole()==1&&query.getAllData()==null){
//                query.setCreatorId(sessionWebUserDto.getUserId());
//            } else {
//            }
            query.setOrderBy("join_time desc");
            List<UserInfo> resultVO = userInfoService.findListByParam(query);
            System.out.println(resultVO);
            return getSuccessResponseVO(resultVO);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }

    @CrossOrigin(origins = Constants.FrontOrigins, allowCredentials = "true")
    @RequestMapping("/getUserInfo")
    @GlobalInterceptor(checkLogin=true)
    public ResponseVO getUserInfo(HttpSession session, HttpServletResponse response) {
//        response.addHeader("Access-Control-Allow-origin",Constants.FrontOrigins);
        try {
            SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
            return getSuccessResponseVO(sessionWebUserDto);
        } catch (Exception e) {
//            throw new BusinessException("图片验证码不正确");
            e.printStackTrace();
//            )
            throw new BusinessException("获取用户信息失败：" + e.getMessage());
        }
    }
    @RequestMapping("/delUserById")
    @GlobalInterceptor(checkLogin=true)
    public ResponseVO delMapById(HttpSession session,@VerifyParam(required = true) String id) {
        SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
        Integer resultVO= null;
        if (sessionWebUserDto.getRole()==1) {
//            验证是否该账户管理员
            resultVO = userInfoService.deleteUserInfoByUserId(id);
        }
        return getSuccessResponseVO(resultVO);
    }
}

